<?php


namespace app\api\controller;


use app\admin\model\LargerActiveCollection;
use think\Controller;
use think\Request;

class LargerActive extends Controller
{
    public function getLargerActiveList(Request $request){
        $res = [];
        $data = $request->post();
        $page = $request->get();

        $largerActive = new \app\admin\model\LargerActive();
        if(!isset($page['page'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        if(isset($data['selected']))$where['name'] = ['like',"%".$data['selected'].'%'];

        $where['status'] = 1;
        $rs = $largerActive->getPageAll($where);

        //收藏和购物车状态
        foreach($rs as $j){
            $j['in_collection'] = 0;
            $j['in_cart'] = 0;
            if(isset($data['user_id'])) {
                //查询收藏
                $whereCollection['user_id'] = $data['user_id'];
                $whereCollection['p_id'] = $j['id'];
                $whereCollection['type'] = 'largerActive';
                $collection = new \app\admin\model\Collection();
                if($collection->getOne($whereCollection)){
                    $j['in_collection'] = 1;
                }
                //查询购物车
                $whereShopCart['user_id'] = $data['user_id'];
                $whereShopCart['pr_type'] = 3;
                $whereShopCart['pr_id'] = $j['id'];
                $shopCart = new \app\admin\model\ShopCart();
                if($shopCart->getOne($whereShopCart)){
                    $j['in_cart'] = 1;
                }
            }
        }

        if($rs){
            $res['data']['code'] = 1;
            $res['data']['list'] = $rs;
        }else{
            $res['data']['code'] = 0;
        }

        $res['status'] = 200;
        echo json_encode($res);
    }

    public function getLargerActiveDetails(Request $request){
        $res = [];
        $data = $request->post();

        $largerActive = new \app\admin\model\LargerActive();
        if(!isset($data['id'])){
            $res['status'] = 400;
            echo json_encode($res);die;
        }

        $where['id'] = $data['id'];
        $rs = $largerActive->getOne($where);
        if($rs){
            //查询是否已添加购物车
            $shopCart = new \app\admin\model\ShopCart();
            $shopWhere['user_id'] = isset($data['user_id']) ? $data['user_id'] : 0;
            $shopWhere['pr_type'] = 1;
            $shopWhere['pr_id'] = $data['id'];
            if($shopCart->getOne($shopWhere)){
                $rs['in_cart'] = 1;
            }else{
                $rs['in_cart'] = '';
            }

            //查询收藏
            $whereCollection['user_id'] = isset($data['user_id']) ? $data['user_id'] : 0;
            $whereCollection['p_id'] = $data['id'];
            $whereCollection['type'] = 'largerActive';
            $collection = new \app\admin\model\Collection();
            if($collection->getOne($whereCollection)){
                $rs['in_collection'] = 1;
            }else{
                $rs['in_collection'] = '';
            }

            $res['data']['code'] = 1;
            $res['data']['list'] = $rs;
        }else{
            $res['data']['code'] = 0;
        }

        $res['status'] = 200;
        echo json_encode($res);
    }
}